<?php
/**
 * Created by PhpStorm.
 * User: Thans
 * Date: 2018/1/3
 * Time: 上午11:01
 */

namespace app\admin\controller;

use app\common\controller\Base;
use app\user\facade\User;
use think\Request;

/**
 * @title 公共类，不验证登录
 * Class PublicController
 * @package app\admin\controller
 */
class PublicController extends Base
{
    /**
     * @var \think\Request Request实例
     */
    protected $request;

    /**
     * @title 构造函数
     * @desc  {"0":"描述：依赖注入request类"}
     */
    public function __construct(Request $request)
    {
        parent::__construct();
        $this->request = $request;
    }

    /**
     * @title 控制器初始化
     * @desc  {"0":"描述：初始化，重定义取消登录、权限验证"}
     */
    public function initialize()
    {

        //取消父初始化，不验证登录
    }

    /**
     * @title 登录接口
     * @desc  {"0":"接口地址：admin/login","1":"请求方式：POST","2":"接口备注：后台登录接口"}
     * @param {"name":"user_login","type":"string","required":true,"default":"","desc":"用户名、邮箱、手机号"}
     * @param {"name":"user_pass","type":"string","required":true,"default":"","desc":"用户密码，明文"}
     * @param {"name":"code","type":"string","required":true,"default":"","desc":"验证码"}
     * @param {"name":"redirect","type":"string","required":false,"default":"","desc":"重定向地址"}
     * @return {"name":"code","type":"number","required":true,"desc":"响应代码","level":1}
     * @return {"name":"msg","type":"string","required":true,"desc":"提示信息","level":1}
     * @return {"name":"date","type":"json","required":true,"desc":"返回数据","level":1}
     * @return {"name":"token","type":"string","required":true,"desc":"登录token","level":2}
     * @return {"name":"redirect","type":"string","required":true,"desc":"应该进入的重定向地址","level":1}
     */
    public function login()
    {
        \thans\log\controller\Log::setTitle('登录后台');
        if (!captcha_check($this->request->post('code'), 'login')) {
            return json([
                'code' => 4004,
                'msg' => '验证码错误',
                'data' => [],
            ]);
        }
        $data['user_login'] = $this->request->post('user_login');
        $data['user_pass'] = $this->request->post('user_pass');
        $result = $this->validate($data,
            'app\admin\validate\UserValidate.user_login');
        if (true !== $result) {
            return [
                'code' => 4003,
                'msg' => $result,
                'data' => [],
            ];
        }
        $data['user_type'] = [1];
        $data['login_ip'] = $this->request->ip();
        return User::login($data, '/admin');
    }
}
